/*
 * Created on Aug 31, 2005 
 */
package net.sf.google.talk.eclipse.views;

import net.sf.google.talk.eclipse.EclipsePlugin;
import net.sf.google.talk.eclipse.preferences.GTalkPreferencePage;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class LoginDialog extends Dialog {
    private String username;
    private String password;
    private Text usernameText;
    private Text passwordText;
    private Button autoConnect;

    protected LoginDialog(Shell parentShell) {
        super(parentShell);
        // TODO Auto-generated constructor stub
    }

    protected Control createContents(Composite parent) {
        // TODO Auto-generated method stub
        parent.setLayout(new GridLayout(2, true));
        Label l = new Label(parent, SWT.NONE);
        l.setText("login:");
        usernameText = new Text(parent, SWT.NONE);
        usernameText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        usernameText.addModifyListener(new ModifyListener() {
            public void modifyText(ModifyEvent e) {
                username = usernameText.getText();
            }});
        
        l = new Label(parent, SWT.NONE);
        l.setText("Password:");
        passwordText = new Text(parent, SWT.PASSWORD);
        passwordText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        passwordText.addModifyListener(new ModifyListener() {
            public void modifyText(ModifyEvent e) {
                password = passwordText.getText();
            }});
        
        autoConnect = new Button(parent, SWT.CHECK);
        autoConnect.setText("&Connect automatically");
//        autoConnect.addSelectionListener(new SelectionListener() {
//            public void widgetSelected(SelectionEvent e) {
//                saveValues = save.getSelection();
//            }
//            public void widgetDefaultSelected(SelectionEvent e) {
//            }});
        
        // Fill defaults with stored preferences
        IPreferenceStore store = EclipsePlugin.getDefault()
		.getPreferenceStore();
        String un = store.getString(GTalkPreferencePage.USERNAME);
        usernameText.setText(un);
        
        usernameText.setText(store.getString(GTalkPreferencePage.USERNAME));
        passwordText.setText(store.getString(GTalkPreferencePage.PASSWORD));
        autoConnect.setSelection(store.getBoolean(GTalkPreferencePage.AUTO_CONNECT));
        
        return super.createContents(parent);
    }

    /**
     * Gets the password.
     * 
     * @return Returns the password.
     */
    public String getPassword() {
        return password;
    }

    /**
     * Sets the password
     *
     * @param password The password to set.
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * Gets the username.
     * 
     * @return Returns the username.
     */
    public String getUsername() {
        return username;
    }

    /**
     * Sets the username
     *
     * @param username The username to set.
     */
    public void setUsername(String username) {
        this.username = username;
    }


    @Override
	protected void okPressed() {
    	IPreferenceStore store = EclipsePlugin.getDefault().getPreferenceStore();
    	store.setValue(GTalkPreferencePage.AUTO_CONNECT, autoConnect.getSelection());
    	store.setValue(GTalkPreferencePage.USERNAME, username);
    	store.setValue(GTalkPreferencePage.PASSWORD, password);
		super.okPressed();
	}


}
